import unittest
from XTestRunner import HTMLTestRunner
from selenium import webdriver
from time import sleep
from selenium.webdriver.common.by import By
import random


class MyTestCase001(unittest.TestCase):
    """客人来到餐厅功能测试"""

    def setUp(self) -> None:

        global driver
        # 初始化浏览器驱动
        driver = webdriver.Chrome()
        # 设置隐式等待
        driver.implicitly_wait(10)
        pass

    def tearDown(self) -> None:
        # 关闭浏览器驱动
        driver.quit()

    def test001(self):
        """客人来到餐厅，随机点击客人入座"""
        driver.get("https://yukuii.top/game")
        # 设置浏览器宽200，高800
        driver.set_window_size(200,800)
        # 游戏背景图过大加载慢停留5s
        sleep(5)
        # 点击开始开始经营按钮
        driver.find_element(By.XPATH,"/html/body/section[1]/div/div[2]").click()
        # 获得客人1盒子
        sleep(3)
        s1 = random.randint(0, 5)
        # //*[@id="waitCustomerBox1"]
        driver.find_element(By.XPATH, '//*[@id="waitCustomerBox{}"]'.format(s1)).click()
        sleep(3)
        customer = driver.find_element(By.XPATH,'//*[@id="waitCustomerBox0"]')
        sleep(5)
        # 通过源码可以看到客人来餐厅是通过调用js使其盒子是否显示
        # 这里通过判断其displayed的布尔值判断客人是否来到餐厅
        Iscome =  customer.is_displayed()
        self.assertTrue(Iscome,"客人来到餐厅")

        # 每一位顾客在餐厅只会停留20s，设计第二个测试用例玩家无操作等待23s
    def test002(self):
        """客人没来到餐厅，场景：玩家无操作等待23s"""
        driver.get("https://yukuii.top/game")
        # 设置浏览器宽200，高800
        driver.set_window_size(200, 800)
        # 游戏背景图过大加载慢停留5s
        sleep(5)
        # 点击开始开始经营按钮
        driver.find_element(By.XPATH, "/html/body/section[1]/div/div[2]").click()
        # 获得客人1盒子
        sleep(5)
        customer = driver.find_element(By.XPATH, '//*[@id="waitCustomerBox0"]')
        sleep(18)
        # 通过源码可以看到客人来餐厅是通过调用js使其盒子是否显示
        # 这里通过判断其displayed的布尔值判断客人是否来到餐厅
        Iscome = customer.is_displayed()
        self.assertFalse(Iscome, "客人没来到餐厅")

class MyTestCase002(unittest.TestCase):
    """厨师上菜、收钱功能测试"""
    def setUp(self) -> None:

        global driver
        # 初始化浏览器驱动
        driver = webdriver.Chrome()
        # 设置隐式等待
        driver.implicitly_wait(10)
        pass

    def tearDown(self) -> None:
        # 关闭浏览器驱动
        driver.quit()


    def test001(self):
        """厨师上菜、收钱功能测试"""
        driver.get("https://yukuii.top/game")
        # 设置浏览器宽200，高800
        driver.set_window_size(200, 800)
        # 游戏背景图过大加载慢停留5s
        sleep(5)
        # 点击开始开始经营按钮
        driver.find_element(By.XPATH, "/html/body/section[1]/div/div[2]").click()
        sleep(2)
        # 点击取消提示窗口
        driver.find_element(By.XPATH, '//*[@id="allCustomerBox"]/div/div[11]').click()
        sleep(2)
        # 取随机值随机点击客人
        s1= random.randint(0,5)
        # //*[@id="waitCustomerBox1"]
        driver.find_element(By.XPATH,'//*[@id="waitCustomerBox{}"]'.format(s1)).click()
        sleep(3)
        # // *[ @ id = "mainDishe1"] 菜单xpath
        s2 = random.randint(1,5)
        driver.find_element(By.XPATH,'//*[@id="mainDishe{}"]'.format(s2)).click()
        sleep(3)
        # 点击 ‘选好了开始上菜’
        driver.find_element(By.XPATH,'/html/body/section[2]/div/div[3]').click()
        sleep(2)
        # 单击取消提示窗口
        driver.find_element(By.XPATH,'//*[@id="allCustomerBox"]/div/div[12]').click()
        sleep(16)
        # // *[ @ id = "customerBox1"] / div[2] / button 上菜按钮xpath
        driver.find_element(By.XPATH,'//*[@id="customerBox1"]/div[2]/button').click()
        sleep(20)
        # // *[ @ id = "customerBox1"] / button[1] 完成用餐收钱xpath
        driver.find_element(By.XPATH,'//*[@id="customerBox1"]/button[1]').click()
        sleep(3)
        # 获取用餐成功文本
        text = driver.find_element(By.XPATH,'//*[@id="allCustomerBox"]/div/div[11]/p').text
        self.assertIn("完成用餐",text,"客人完成用餐")

    def test002(self):
        """客人点菜后，厨师不上菜，客人离开餐厅功能测试"""
        driver.get("https://yukuii.top/game")
        # 设置浏览器宽200，高800
        driver.set_window_size(200, 800)
        # 游戏背景图过大加载慢停留5s
        sleep(5)
        # 点击开始开始经营按钮
        driver.find_element(By.XPATH, "/html/body/section[1]/div/div[2]").click()
        sleep(2)
        # 点击取消提示窗口
        driver.find_element(By.XPATH, '//*[@id="allCustomerBox"]/div/div[11]').click()
        sleep(2)
        # 取随机值随机点击客人
        s1 = random.randint(0, 5)
        # //*[@id="waitCustomerBox1"]
        driver.find_element(By.XPATH, '//*[@id="waitCustomerBox{}"]'.format(s1)).click()
        sleep(3)
        # // *[ @ id = "mainDishe1"] 菜单xpath
        s2 = random.randint(1, 5)
        driver.find_element(By.XPATH, '//*[@id="mainDishe{}"]'.format(s2)).click()
        sleep(3)
        # 点击 ‘选好了开始上菜’
        driver.find_element(By.XPATH, '/html/body/section[2]/div/div[3]').click()
        sleep(2)
        # 单击取消提示窗口
        driver.find_element(By.XPATH, '//*[@id="allCustomerBox"]/div/div[12]').click()
        sleep(25)
        driver.find_element(By.XPATH,'//*[@id="customerBox1"]/button[2]').click()
        sleep(2)
        text = driver.find_element(By.XPATH,'//*[@id="allCustomerBox"]/div/div[11]/p').text
        sleep(2)
        self.assertIn("失望而归",text,"客人失望而归")






if __name__ == '__main__':
    # 创建unittest测试套件
    suitcase = unittest.TestSuite()
    suitcase.addTests([MyTestCase001('test001'),MyTestCase001('test002'),
                       MyTestCase002('test001'),MyTestCase002('test002')])
    # HTMLTestRunner我总报错，查询后说是Python3不兼容
    # 所以我直接换了一个生成HTML测试报告的框架XTestRunner
    # https://github.com/SeldomQA/XTestRunner
    with (open('./厨房游戏功能测试报告.html','wb')) as file:
        runner = HTMLTestRunner(
            stream=file,
            title="厨房游戏功能测试报告",
            description=['类型：Web自动化测试', '操作系统：Windows', '浏览器：Chrome','测试人员：欧阳泽宇'],
            language="zh-CN",
            tester="欧阳泽宇"
        )
        runner.run(testlist=suitcase)

